package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.statistics;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.bo.InvoiceStatisticsBo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.bo.PoundQueryForAdjustPriceBo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.weightSheets.AshGateHandling;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.InvoiceStatisticsExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.PoundQueryExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.QueryWeightSheetsExcelEn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.mapper.CWeightSheetsMapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.Constructor.CWeightSheets2GetCWeightSheetsVo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.GetCWeightSheetsVo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.InvoiceStatisticsVo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.MonthlySalesVo;
import com.xiaoxu.intranetweb.util.SzResult;
import com.xiaoxu.intranetweb.util.excel.strategy.ExcelWidthStyleStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.Future;
import java.util.stream.Collectors;


/**
 * 磅单查询业务层
 *
 * @author xiaoxu
 * @version 1.0
 * @date 2023/1/13 15:00
 * @since 1.0
 */
@Component
@Slf4j
public class PoundQueryBu {
    @Autowired
    private ICWeightSheetsService cWeightSheetsService;
    @Autowired
    private CWeightSheetsMapper cWeightSheetsMapper;
    @Autowired
    private CWeightSheets cWeightSheets;
    @Autowired
    private ICProductsInfoService cProductsInfoService;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private ICTrucksService cTrucksService;
    @Autowired
    private ICIcCardsInfoService cIcCardsInfoService;
    @Autowired
    private CWeightSheets2GetCWeightSheetsVo cWeightSheets2GetCWeightSheetsVo;
    @Autowired
    private ICAshGateVideoBindService iCAshGateVideoBindService;
    @Autowired
    private ICCompanysService  cCompanysService;
    @Autowired
    private ICIcCardsInfoService iCIcCardsInfoService;
//分页查询所有数据,根据单号排序
//    根据时间起止（重车时间）查询，根据单号排序

    /**
     * 根据时间起止（重车时间）查询，根据单号排序
     *
     * @param page          页码
     * @param pageSize      每页显示条数
     * @param fullTimeStart 重车起始时间
     * @param fullTimeEnd   重车结束时间
     * @param coSupply      供货单位
     * @param coSupplyChi   供货单位中文
     * @param co            供货单位
     * @param chiCo         供货单位中文
     * @param product       产品
     * @param truckno       车牌号
     * @param operator      操作员
     * @param sheetno       单号
     * @param reason        原因
     * @param iccardno      IC卡号
     * @param isEmpty       是否为空
     * @return com.xiaoxu.intranetweb.util.SzResult
     */
    public SzResult getCWeightSheetsByTime(String page, String pageSize, String token, String fullTimeStart, String fullTimeEnd,
                                           String coSupply, String coSupplyChi, String co, String chiCo, List<String> product, String truckno, String operator, String sheetno, String reason, String iccardno,String iccardAlias, Integer isEmpty, String plantId) throws Exception {
        //        if (product != null && !product.isEmpty()) {
//            for (String s : product) {
//                QueryWrapper<CProductsInfo> pdtQueryWrapper = new QueryWrapper<>();
//                pdtQueryWrapper.eq("PDTINF_PDT", s);
//                pdtQueryWrapper.eq("PDTINF_DELETED", 0);
//                pdtQueryWrapper.eq("DATA_FROM", plantId);
//                CProductsInfo one = cProductsInfoService.getOne(pdtQueryWrapper);
//                if (one != null) {
//                    pdtinfPdt = one.getPdtinfPdt();
//                }
//            }
//        }
        if (page == null || page.equals("") && pageSize == null || pageSize.equals("")) {
            QueryWrapper<CWeightSheets> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("WEISHT_FULL_TM");
//        根据时间重车起止
            queryWrapper.between(fullTimeStart != null && !fullTimeStart.equals("") && fullTimeEnd != null && !fullTimeEnd.equals(""), "WEISHT_FULL_TM", fullTimeStart, fullTimeEnd);
//        开始时间往后
            queryWrapper.ge(fullTimeStart != null && !fullTimeStart.equals(""), "WEISHT_FULL_TM", fullTimeStart);
//        结束时间往前
            queryWrapper.le(fullTimeEnd != null && !fullTimeEnd.equals(""), "WEISHT_FULL_TM", fullTimeEnd);
//WEISHT_BACKED不等于0表示车辆已经回磅
            queryWrapper.eq("WEISHT_BACKED", 1);
//        不返回已删除的榜单
            queryWrapper.ne("WEISHT_DELETED", "1");
//        根据供货单位查询
            queryWrapper.like(coSupply != null && !coSupply.equals(""), "WEISHT_CO_SUPPLY", coSupply);
            queryWrapper.eq(coSupplyChi != null && !coSupplyChi.equals(""), "WEISHT_CO_SUPPLY_CHI", coSupplyChi);
//        根据收货单位查询
            queryWrapper.eq(co != null && !co.equals(""), "WEISHT_CO", co);
            queryWrapper.eq(chiCo != null && !chiCo.equals(""), "WEISHT_CHI_CO", chiCo);
//        根据卡号查询
            queryWrapper.eq(iccardno != null && !iccardno.equals(""), "WEISHT_ICCARDNO", iccardno);
            if (iccardAlias !=null && !iccardAlias.equals("")) {
                QueryWrapper<CIcCardsInfo> queryWrapper3 = new QueryWrapper<>();
                queryWrapper3.eq("CARDINF_DELETED", 0);
                queryWrapper3.eq("DATA_FROM", plantId);
                queryWrapper3.like("CARDINF_ALIAS", iccardAlias);
                List<CIcCardsInfo> cIcCardsInfos = iCIcCardsInfoService.list(queryWrapper3);
                if (cIcCardsInfos.size()>0) {
                    List<String> list = new ArrayList<>();
                    for (CIcCardsInfo cIcCardsInfo : cIcCardsInfos) {
                        list.add(cIcCardsInfo.getCardinfCardno());
                    }
                    queryWrapper.in(  "WEISHT_ICCARDNO", list);
                }
            }
//        根据品种查询
            queryWrapper.in(product != null && !product.isEmpty(), "WEISHT_PRODUCT", product);
//        根据车号查询
            queryWrapper.like(truckno != null && !truckno.equals(""), "WEISHT_TRUCKNO", truckno);
//        根据操作员查询
            queryWrapper.like(operator != null && !operator.equals(""), "WEISHT_OPERATOR", operator);
//        根据单号查询
            queryWrapper.like(sheetno != null && !sheetno.equals(""), "WEISHT_SHEETNO", sheetno);
//        根据1备注查询
            queryWrapper.like(reason != null && !reason.equals(""), "WEISHT_MODIFY_REASON", reason);
            queryWrapper.eq("DATA_FROM", plantId);
//        根据是否空车查询
            if (isEmpty != null) {
                if (isEmpty == 0) {
//                WEISHT_NET_WGHT大于0表示重车
                    queryWrapper.ne("WEISHT_NET_WGHT", 0);
                }
            }
            List<CWeightSheets> list = cWeightSheetsService.list(queryWrapper);
//
//            PoundQueryForAdjustPriceBo poundQueryForAdjustPriceBo = new PoundQueryForAdjustPriceBo();
//            poundQueryForAdjustPriceBo.setPage(page);
//            poundQueryForAdjustPriceBo.setPageSize(pageSize);
//            poundQueryForAdjustPriceBo.setFullTimeStart(fullTimeStart);
//            poundQueryForAdjustPriceBo.setFullTimeEnd(fullTimeEnd);
//            poundQueryForAdjustPriceBo.setCoSupply(coSupply);
//            poundQueryForAdjustPriceBo.setCoSupplyChi(coSupplyChi);
//            poundQueryForAdjustPriceBo.setCo(co);
//            poundQueryForAdjustPriceBo.setChiCo(chiCo);
//            if (product !=null && product.size()>0) {
//                poundQueryForAdjustPriceBo.setProduct(product.get(0));
//            }
//            poundQueryForAdjustPriceBo.setTruckno(truckno);
//            poundQueryForAdjustPriceBo.setOperator(operator);
//            poundQueryForAdjustPriceBo.setSheetno(sheetno);
//            poundQueryForAdjustPriceBo.setReason(reason);
//            poundQueryForAdjustPriceBo.setIccardno(iccardno);
//            poundQueryForAdjustPriceBo.setIsEmpty(isEmpty);
//            List<GetCWeightSheetsVo> getCWeightSheetss = cWeightSheetsService.poundQueryForAdjustPrice(poundQueryForAdjustPriceBo, plantId);

//            List<GetCWeightSheetsVo> getCWeightSheetsVoList = new ArrayList<>();
//            for (CWeightSheets cWeightSheets : getCWeightSheetss) {
//                GetCWeightSheetsVo getCWeightSheetsVo =cWeightSheets2GetCWeightSheetsVo.getCWeightSheetsVo(cWeightSheets);
//                getCWeightSheetsVoList.add(getCWeightSheetsVo);
//            }
//            int total = getCWeightSheetss.size();
            Map map = new HashMap();
            map.put("total", list.size());
            map.put("items", list);
            SzResult szResult = new SzResult();
            szResult.setCode(200);
            szResult.setMsg("查询成功");
            szResult.setResult(map);
            return szResult;
        } else {
//新建一个map集合，用来存放查询条件
            Page<CWeightSheets> page1 = new Page<>(Integer.parseInt(page), Integer.parseInt(pageSize));
            QueryWrapper<CWeightSheets> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("WEISHT_FULL_TM");
//        根据时间重车起止
            queryWrapper.between(fullTimeStart != null && !fullTimeStart.equals("") && fullTimeEnd != null && !fullTimeEnd.equals(""), "WEISHT_FULL_TM", fullTimeStart, fullTimeEnd);
//        开始时间往后
            queryWrapper.ge(fullTimeStart != null && !fullTimeStart.equals(""), "WEISHT_FULL_TM", fullTimeStart);
//        结束时间往前
            queryWrapper.le(fullTimeEnd != null && !fullTimeEnd.equals(""), "WEISHT_FULL_TM", fullTimeEnd);
//WEISHT_BACKED不等于0表示车辆已经回磅
            queryWrapper.eq("WEISHT_BACKED", 1);
//        不返回已删除的榜单
            queryWrapper.eq("WEISHT_DELETED", 0);
//        根据供货单位查询
            queryWrapper.eq(coSupply != null && !coSupply.equals(""), "WEISHT_CO_SUPPLY", coSupply);
            queryWrapper.eq(coSupplyChi != null && !coSupplyChi.equals(""), "WEISHT_CO_SUPPLY_CHI", coSupplyChi);
//        根据收货单位查询
            queryWrapper.eq(co != null && !co.equals(""), "WEISHT_CO", co);
            queryWrapper.eq(chiCo != null && !chiCo.equals(""), "WEISHT_CHI_CO", chiCo);
//        根据卡号查询
            queryWrapper.eq(iccardno != null && !iccardno.equals(""), "WEISHT_ICCARDNO", iccardno);
            if (iccardAlias !=null && !iccardAlias.equals("")) {
                QueryWrapper<CIcCardsInfo> queryWrapper3 = new QueryWrapper<>();
                queryWrapper3.eq("CARDINF_DELETED", 0);
                queryWrapper3.eq("DATA_FROM", plantId);
                queryWrapper3.like("CARDINF_ALIAS", iccardAlias);
                List<CIcCardsInfo> cIcCardsInfos = iCIcCardsInfoService.list(queryWrapper3);
                if (cIcCardsInfos.size()>0) {
                    List<String> list = new ArrayList<>();
                    for (CIcCardsInfo cIcCardsInfo : cIcCardsInfos) {
                        list.add(cIcCardsInfo.getCardinfCardno());
                    }
                    queryWrapper.in(  "WEISHT_ICCARDNO", list);
                }
            }
//        根据品种查询
            queryWrapper.in(product != null && product.size() > 0, "WEISHT_PRODUCT", product);
//        根据车号查询
            queryWrapper.like(truckno != null && !truckno.equals(""), "WEISHT_TRUCKNO", truckno);
//        根据操作员查询
            queryWrapper.like(operator != null && !operator.equals(""), "WEISHT_OPERATOR", operator);
//        根据单号查询
            queryWrapper.like(sheetno != null && !sheetno.equals(""), "WEISHT_SHEETNO", sheetno);
//        根据1备注查询
            queryWrapper.like(reason != null && !reason.equals(""), "WEISHT_MODIFY_REASON", reason);
            queryWrapper.eq("DATA_FROM", plantId);
//        根据是否空车查询
            if (isEmpty != null) {
                if (isEmpty == 0) {
//                WEISHT_NET_WGHT大于0表示重车
                    queryWrapper.ne("WEISHT_NET_WGHT", 0);
                }
            }
//     返回查询到的数据
            IPage<CWeightSheets> iPage = cWeightSheetsService.page(page1, queryWrapper);
            List<CWeightSheets> cWeightSheetsList = iPage.getRecords();
            List<GetCWeightSheetsVo> getCWeightSheetsVoList = new ArrayList<>();
            for (CWeightSheets cWeightSheets : cWeightSheetsList) {
                GetCWeightSheetsVo getCWeightSheetsVo = cWeightSheets2GetCWeightSheetsVo.getCWeightSheetsVo(cWeightSheets);
                getCWeightSheetsVoList.add(getCWeightSheetsVo);
            }
            //        返回查询到的总条数，先给前端去分页，效率高的一批
            Long total = iPage.getTotal();
            Map map = new HashMap();
            map.put("total", total);
            map.put("items", getCWeightSheetsVoList);
            SzResult szResult = new SzResult();
            szResult.setCode(200);
            szResult.setMsg("查询成功");
            szResult.setResult(map);
            return szResult;
        }
    }


    public SzResult getWeightSheets (String token, String fullTimeStart, String fullTimeEnd,
                                           String coSupply, String coSupplyChi, String co, String chiCo, String product, String truckno, String operator, String sheetno, String reason, String iccardno, Integer isEmpty, String plantId) throws Exception {
        QueryWrapper<CWeightSheets> queryWrapper = new QueryWrapper<>();
//        queryWrapper.between(fullTimeStart != null && !fullTimeStart.equals("") && fullTimeEnd != null && !fullTimeEnd.equals(""), "WEISHT_FULL_TM", fullTimeStart, fullTimeEnd);
//        开始时间往后
        queryWrapper.ge(fullTimeStart != null && !fullTimeStart.equals(""), "WEISHT_FULL_TM", fullTimeStart);
//        结束时间往前
        queryWrapper.le(fullTimeEnd != null && !fullTimeEnd.equals(""), "WEISHT_FULL_TM", fullTimeEnd);
//WEISHT_BACKED不等于0表示车辆已经回磅
        queryWrapper.eq("WEISHT_BACKED", 1);
//        不返回已删除的榜单
        queryWrapper.eq("WEISHT_DELETED", 0);
//        根据供货单位查询
        queryWrapper.eq(coSupply != null && !coSupply.equals(""), "WEISHT_CO_SUPPLY", coSupply);
        queryWrapper.eq(coSupplyChi != null && !coSupplyChi.equals(""), "WEISHT_CO_SUPPLY_CHI", coSupplyChi);
//        根据收货单位查询
        queryWrapper.eq(co != null && !co.equals(""), "WEISHT_CO", co);
        queryWrapper.eq(chiCo != null && !chiCo.equals(""), "WEISHT_CHI_CO", chiCo);
//        根据卡号查询
        queryWrapper.eq(iccardno != null && !iccardno.equals(""), "WEISHT_ICCARDNO", iccardno);
//        根据品种查询
        queryWrapper.eq(product != null && !product.equals(""), "WEISHT_PRODUCT", product);
//        根据车号查询
        queryWrapper.like(truckno != null && !truckno.equals(""), "WEISHT_TRUCKNO", truckno);
//        根据操作员查询
        queryWrapper.like(operator != null && !operator.equals(""), "WEISHT_OPERATOR", operator);
//        根据单号查询
        queryWrapper.like(sheetno != null && !sheetno.equals(""), "WEISHT_SHEETNO", sheetno);
//        根据1备注查询
        queryWrapper.like(reason != null && !reason.equals(""), "WEISHT_MODIFY_REASON", reason);
        queryWrapper.eq("DATA_FROM", plantId);
//        根据是否空车查询
        if (isEmpty != null) {
            if (isEmpty == 0) {
//                WEISHT_NET_WGHT大于0表示重车
                queryWrapper.ne("WEISHT_NET_WGHT", 0);
            }
        }
        queryWrapper.orderByDesc("WEISHT_FULL_TM");
//     返回查询到的数据
        List<QueryWeightSheetsExcelEn> queryWeightSheetsExcelEnList = cWeightSheetsService.list(queryWrapper).stream().map(item->{
            QueryWeightSheetsExcelEn queryWeightSheetsExcelEn = new QueryWeightSheetsExcelEn();
            BeanUtils.copyProperties(item,queryWeightSheetsExcelEn);
            queryWeightSheetsExcelEn.setWeishtCoSupply(item.getWeishtCoSupply()+"."+item.getWeishtCoSupplyChi());
            queryWeightSheetsExcelEn.setWeishtCo(item.getWeishtCo()+item.getWeishtChiCo());
            return queryWeightSheetsExcelEn;
        }).collect(Collectors.toList());
        SzResult szResult = new SzResult();
        szResult.setCode(200);
        szResult.setMsg("查询成功");
        szResult.setResult(queryWeightSheetsExcelEnList);
        return szResult;
    }

    public void getWeightSheetsExcel(String token, String fullTimeStart, String fullTimeEnd,
                                         String coSupply, String coSupplyChi, String co, String chiCo, String product, String truckno, String operator, String sheetno, String reason, String iccardno,String iccardAlias, Integer isEmpty, String plantId, HttpServletResponse response) throws Exception {
        QueryWrapper<CWeightSheets> queryWrapper = new QueryWrapper<>();
//        queryWrapper.between(fullTimeStart != null && !fullTimeStart.equals("") && fullTimeEnd != null && !fullTimeEnd.equals(""), "WEISHT_FULL_TM", fullTimeStart, fullTimeEnd);
//        开始时间往后
        queryWrapper.ge(fullTimeStart != null && !fullTimeStart.equals(""), "WEISHT_FULL_TM", fullTimeStart);
//        结束时间往前
        queryWrapper.le(fullTimeEnd != null && !fullTimeEnd.equals(""), "WEISHT_FULL_TM", fullTimeEnd);
//WEISHT_BACKED不等于0表示车辆已经回磅
        queryWrapper.eq("WEISHT_BACKED", 1);
//        不返回已删除的榜单
        queryWrapper.eq("WEISHT_DELETED", 0);
//        根据供货单位查询
        queryWrapper.eq(coSupply != null && !coSupply.equals(""), "WEISHT_CO_SUPPLY", coSupply);
        queryWrapper.eq(coSupplyChi != null && !coSupplyChi.equals(""), "WEISHT_CO_SUPPLY_CHI", coSupplyChi);
//        根据收货单位查询
        queryWrapper.eq(co != null && !co.equals(""), "WEISHT_CO", co);
        queryWrapper.eq(chiCo != null && !chiCo.equals(""), "WEISHT_CHI_CO", chiCo);
//        根据卡号查询
        queryWrapper.eq(iccardno != null && !iccardno.equals(""), "WEISHT_ICCARDNO", iccardno);
        if (iccardAlias !=null && !iccardAlias.equals("")) {
            QueryWrapper<CIcCardsInfo> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("CARDINF_DELETED", 0);
            queryWrapper3.eq("DATA_FROM", plantId);
            queryWrapper3.like("CARDINF_ALIAS", iccardAlias);
            List<CIcCardsInfo> cIcCardsInfos = iCIcCardsInfoService.list(queryWrapper3);
            if (cIcCardsInfos.size()>0) {
                List<String> list = new ArrayList<>();
                for (CIcCardsInfo cIcCardsInfo : cIcCardsInfos) {
                    list.add(cIcCardsInfo.getCardinfCardno());
                }
                queryWrapper.in(  "WEISHT_ICCARDNO", list);
            }
        }
//        根据品种查询
        queryWrapper.eq(product != null && !product.equals(""), "WEISHT_PRODUCT", product);
//        根据车号查询
        queryWrapper.like(truckno != null && !truckno.equals(""), "WEISHT_TRUCKNO", truckno);
//        根据操作员查询
        queryWrapper.like(operator != null && !operator.equals(""), "WEISHT_OPERATOR", operator);
//        根据单号查询
        queryWrapper.like(sheetno != null && !sheetno.equals(""), "WEISHT_SHEETNO", sheetno);
//        根据1备注查询
        queryWrapper.like(reason != null && !reason.equals(""), "WEISHT_MODIFY_REASON", reason);
        queryWrapper.eq("DATA_FROM", plantId);
//        根据是否空车查询
        if (isEmpty != null) {
            if (isEmpty == 0) {
//                WEISHT_NET_WGHT大于0表示重车
                queryWrapper.ne("WEISHT_NET_WGHT", 0);
            }
        }
        queryWrapper.orderByDesc("WEISHT_FULL_TM");
//     返回查询到的数据
        List<QueryWeightSheetsExcelEn> queryWeightSheetsExcelEnList = cWeightSheetsService.list(queryWrapper).stream().map(item->{
            QueryWeightSheetsExcelEn queryWeightSheetsExcelEn = new QueryWeightSheetsExcelEn();

            CAshGateVideoBind cAshGateVideoBind = new CAshGateVideoBind();  // 假设这是正确的构造函数

            CAshGateVideoBind cAshGateVideoBind1 = null;
            if (item.getWeishtSelAshgateId() != null) {
                QueryWrapper<CAshGateVideoBind> cAshGateVideoBindQueryWrapper1 = new QueryWrapper<>();
                cAshGateVideoBindQueryWrapper1.eq("ASHGATE_ID", item.getWeishtSelAshgateId());
                cAshGateVideoBindQueryWrapper1.eq("ASHGATE_DELETED", 0);
                cAshGateVideoBind1 = iCAshGateVideoBindService.getOne(cAshGateVideoBindQueryWrapper1);
            }
            // 查询实装点
            CAshGateVideoBind cAshGateVideoBind2 = null;
            if (item.getWeishtAshgateId() != null) {
                QueryWrapper<CAshGateVideoBind> cAshGateVideoBindQueryWrapper2 = new QueryWrapper<>();
                cAshGateVideoBindQueryWrapper2.eq("ASHGATE_ID", item.getWeishtAshgateId());
                cAshGateVideoBindQueryWrapper2.eq("ASHGATE_DELETED", 0);
                cAshGateVideoBind2 = iCAshGateVideoBindService.getOne(cAshGateVideoBindQueryWrapper2);
            }
            AshGateHandling ashGateHandling = new  AshGateHandling();

            // 获取产品名称
            String pdtName = cWeightSheets.getWeishtProduct();
            if (pdtName == null) {
                pdtName = "";  // 或者其他默认值
            }

            // 获取装载点名称
            String ashGateName1 = (cAshGateVideoBind1 != null) ? cAshGateVideoBind1.getAshgateName() : "";
            String ashGateName2 = (cAshGateVideoBind2 != null) ? cAshGateVideoBind2.getAshgateName() : "";

            // 调用 ashGateHandling 方法
            String ashGateName = ashGateHandling.ashGateHandling(pdtName, ashGateName1, ashGateName2);
            // 设置装载点名称
            cAshGateVideoBind.setAshgateName(ashGateName != null ? ashGateName : "");
            BeanUtils.copyProperties(item,queryWeightSheetsExcelEn);
            QueryWrapper<CIcCardsInfo> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("CARDINF_CARDNO", item.getWeishtIccardno());
            queryWrapper2.eq("CARDINF_DELETED", 0);
            CIcCardsInfo cIcCardsInfo = iCIcCardsInfoService.getOne(queryWrapper2);
            if (cIcCardsInfo != null) {
                queryWeightSheetsExcelEn.setIccardAlias(cIcCardsInfo.getCardinfAlias());
            }
//            queryWeightSheetsExcelEn.setWeishtCoSupply(item.getWeishtCoSupply()+item.getWeishtCoSupplyChi());
//            queryWeightSheetsExcelEn.setWeishtCo(item.getWeishtCo()+item.getWeishtChiCo());
          queryWeightSheetsExcelEn.setWeishtCoSupply(item.getWeishtCoSupply());
           queryWeightSheetsExcelEn.setWeishtCo(item.getWeishtCo());

            queryWeightSheetsExcelEn.setWeishtAshgateName(cAshGateVideoBind.getAshgateName());
            queryWeightSheetsExcelEn.setWeishtTell(item.getWeishtTell());
            queryWeightSheetsExcelEn.setWeishtIdentity(item.getWeishtIdentity());
            return queryWeightSheetsExcelEn;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel; charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(co+chiCo+product+"磅单", "UTF-8") + ".xlsx");
        EasyExcel.write(response.getOutputStream(), QueryWeightSheetsExcelEn.class)
                .registerWriteHandler(new ExcelWidthStyleStrategy(0))//自动列宽
                .sheet(co+chiCo+product+"磅单")
                .doWrite(queryWeightSheetsExcelEnList);
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }

//查询出所有品种

    /**
     * 查询出所有品种
     *
     * @return
     */
    public Map findAllProduct() {
//c_products_info表
        QueryWrapper<CProductsInfo> queryWrapper = new QueryWrapper<>();
//        定义一个map
        Map<String, Object> map = new HashMap<>();
//        拿到所有值
        List<CProductsInfo> cProductsInfoList = cProductsInfoService.list(queryWrapper);
//       遍历cProductsInfoList拿到PDTINF_STOCKCLS的值为1的PDTINF_PDT的值,进货
        List<String> list = cProductsInfoList.stream().filter(cProductsInfo -> cProductsInfo.getPdtinfStockcls() == 1).map(CProductsInfo::getPdtinfPdt).collect(Collectors.toList());
//        过滤掉list重复的值
        List<String> list1 = list.stream().distinct().collect(Collectors.toList());
        map.put("Purchase", list1);
        //       遍历cProductsInfoList拿到PDTINF_STOCKCLS的值为0的PDTINF_PDT的值,出货
        List<String> list2 = cProductsInfoList.stream().filter(cProductsInfo -> cProductsInfo.getPdtinfStockcls() == 0).map(CProductsInfo::getPdtinfPdt).collect(Collectors.toList());
        //        过滤掉list2重复的值
        List<String> list3 = list2.stream().distinct().collect(Collectors.toList());
        map.put("Shipment", list3);
        return map;
    }
//    单开一个线程导出excel

    /**
     * 单开一个线程导出excel
     *
     * @param fields    传入的字段
     * @param filename  文件名
     * @param ids       传入的id
     * @param isHeader  是否有表头
     * @param mode      导出模式
     * @param original  是否原始数据
     * @param pager     分页
     * @param sheetName sheet名
     * @param token     token
     * @return 返回一个Future
     */

    public List<PoundQueryExcelEn> exportExcel(List<Map<String, Object>> fields, String filename, List<Map<String, Object>> ids, Boolean isHeader, String mode, Boolean original, Map<String, String> pager, String sheetName, String token, String fullTimeStart, String fullTimeEnd,
                                                       String coSupply, String coSupplyChi, String co, String chiCo, List<String> product, String truckno, String operator, String sheetno, String reason, String iccardno,String iccardAlias, Integer isEmpty, String plantId) {
        List<Map<String, Object>> newIds = new ArrayList<>();
//        最终数据
        List<PoundQueryExcelEn> poundQueryExcelEns = new ArrayList<>();
// 判断 mode为"current"时，导出当前页，为"all"时，导出所有页，selected时，导出选中数据
        try {
            switch (mode) {
                case "current":
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            if (id.containsKey(field.get("field"))) {
                                newId.put(field.get("field").toString(), id.get(field.get("field")));
                            }
                        }
                        newId.put("weishtChiCo", id.get("weishtChiCo"));
                        newId.put("weishtCoSupplyChi", id.get("weishtCoSupplyChi"));
                        newIds.add(newId);
                    }
//            newIds为Map<String, Object>类型，需要转换为PoundQueryExcelBo类型
//            遍历newIds
                    for (Map<String, Object> newId : newIds) {
//                创建一个PoundQueryExcelBo对象
                        PoundQueryExcelEn poundQueryExcelEn = PoundQueryExcelEn.builder().build();
//                遍历newId
                        for (Map.Entry<String, Object> entry : newId.entrySet()) {
//                    判断entry的key是否为"poundDate"，是的话，将entry的value转换为LocalDateTime类型，再赋值给poundQueryExcelBo的poundDate
                            if (entry.getKey().equals("weishtSheetno")) {
                                poundQueryExcelEn.setWeishtSheetno(entry.getValue().toString());
                            }
//                            weishtYearSn
                            if (entry.getKey().equals("weishtYearSn")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtYearSn(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("weishtTruckno")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtTruckno(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("weishtProduct")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtProduct(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("weishtGrossWght")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtGrossWght(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("weishtTare")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtTare(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("weishtNetWght")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtNetWght(new BigDecimal(entry.getValue().toString()));
                                }
                            }
//                            weishtBillgross
                            if (entry.getKey().equals("weishtBillgross")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtBillgross(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("weishtBilltare")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtBilltare(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("weishtBillnet")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtBillnet(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("weishtPrice")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtPrice(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("weishtMarketPrice")){
                                if ( (entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtMarketPrice(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("weightSpecialPrice")){
                                if ( (entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeightSpecialPrice(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("weishtPriceType")){
                                if ( (entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtPriceType(entry.getValue().toString());
                                }
                            }

                            if (entry.getKey().equals("weishtMoney")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtMoney(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("weishtCoSupply")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtCoSupply(entry.getValue().toString());
                                }
                                //poundQueryExcelEn.setWeishtCoSupply(entry.getValue().toString() + "." + poundQueryExcelEn.getWeishtCoSupply());
                            }
                            if (entry.getKey().equals("weishtCo")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtCo(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("weishtEnterTm")) {
                                if (entry.getValue() != null) {
                                    poundQueryExcelEn.setWeishtEnterTm(entry.getValue().toString().replace("T"," "));
                                } else {
                                    poundQueryExcelEn.setWeishtEnterTm("");
                                }
                            }
                            if (entry.getKey().equals("weishtOutTm")) {
                                if (entry.getValue() != null) {
                                    poundQueryExcelEn.setWeishtOutTm(entry.getValue().toString().replace("T"," "));
                                } else {
                                    poundQueryExcelEn.setWeishtOutTm("");
                                }
                            }
                            if (entry.getKey().equals("weishtEmptyTm")) {
                                if (entry.getValue() != null) {
                                    poundQueryExcelEn.setWeishtEmptyTm(entry.getValue().toString().replace("T"," "));
                                } else {
                                    poundQueryExcelEn.setWeishtEmptyTm("");
                                }

                            }
                            if (entry.getKey().equals("weishtFullTm")) {
                                if (entry.getValue() != null) {
                                    poundQueryExcelEn.setWeishtFullTm(entry.getValue().toString().replace("T"," "));
                                } else {
                                    poundQueryExcelEn.setWeishtFullTm("");
                                }
                            }
                            if (entry.getKey().equals("weishtLoadTm")) {
                                if (entry.getValue() != null) {
                                    poundQueryExcelEn.setWeishtLoadTm(entry.getValue().toString().replace("T"," "));
                                } else {
                                    poundQueryExcelEn.setWeishtLoadTm("");
                                }
                            }
                            if (entry.getKey().equals("weishtLoadEndTm")) {
                                if (entry.getValue() != null) {
                                    poundQueryExcelEn.setWeishtLoadEndTm(entry.getValue().toString().replace("T"," "));
                                } else {
                                    poundQueryExcelEn.setWeishtLoadEndTm("");
                                }
                            }
//                            weishtIccardno
                            if (entry.getKey().equals("weishtIccardno")) {
                                if (entry.getValue() != null) {
                                    poundQueryExcelEn.setWeishtIccardno(entry.getValue().toString());
                                } else {
                                    poundQueryExcelEn.setWeishtIccardno("");
                                }
                            }
//                            weishtIccardAlias
                            if (entry.getKey().equals("weishtIccardAlias")) {
                                if (entry.getValue() != null) {
                                    poundQueryExcelEn.setWeishtIccardAlias(entry.getValue().toString());
                                } else {
                                    poundQueryExcelEn.setWeishtIccardAlias("");
                                }
                            }
                            if (entry.getKey().equals("weishtRemark")) {
                                if (entry.getValue() != null) {
                                    poundQueryExcelEn.setWeishtRemark(entry.getValue().toString());
                                } else {
                                    poundQueryExcelEn.setWeishtRemark("");
                                }
                            }
//                            if (entry.getKey().equals("weishtOperator")) {
//                                if (entry.getValue() != null) {
//                                    poundQueryExcelEn.setWeishtOperator(entry.getValue().toString());
//                                } else {
//                                    poundQueryExcelEn.setWeishtOperator("");
//                                }
//                            }
                            //                            weishtAshgateName
                            if (entry.getKey().equals("weishtAshgateName")) {
                                if (entry.getValue() != null) {
                                    poundQueryExcelEn.setWeishtAshgateName(entry.getValue().toString());
                                } else {
                                    poundQueryExcelEn.setWeishtAshgateName("");
                                }
                            }
                        }
//                存入poundQueryExcelBos
                        poundQueryExcelEns.add(poundQueryExcelEn);
                    }
                    break;
                case "all":
//                    为全部时直接调用查询方法
                    QueryWrapper<CWeightSheets> queryWrapper = new QueryWrapper<>();
                    queryWrapper.orderByDesc("WEISHT_FULL_TM");
//        根据时间重车起止
                    queryWrapper.between(fullTimeStart != null && !fullTimeStart.equals("") && fullTimeEnd != null && !fullTimeEnd.equals(""), "WEISHT_FULL_TM", fullTimeStart, fullTimeEnd);
//        开始时间往后
                    queryWrapper.ge(fullTimeStart != null && !fullTimeStart.equals(""), "WEISHT_FULL_TM", fullTimeStart);
//        结束时间往前
                    queryWrapper.le(fullTimeEnd != null && !fullTimeEnd.equals(""), "WEISHT_FULL_TM", fullTimeEnd);
//WEISHT_BACKED不等于0表示车辆已经回磅
                    queryWrapper.ne("WEISHT_BACKED", 0);
//        不返回已删除的榜单
                    queryWrapper.ne("WEISHT_DELETED", "1");
//        根据供货单位查询
                    queryWrapper.like(coSupply != null && !coSupply.equals(""), "WEISHT_CO_SUPPLY", coSupply);
                    queryWrapper.eq(coSupplyChi != null && !coSupplyChi.equals(""), "WEISHT_CO_SUPPLY_CHI", coSupplyChi);
//        根据收货单位查询
                    queryWrapper.eq(co != null && !co.equals(""), "WEISHT_CO", co);
//        根据卡号查询
                    queryWrapper.eq(iccardno != null && !iccardno.equals(""), "WEISHT_ICCARDNO", iccardno);
                    if (iccardAlias !=null && !iccardAlias.equals("")) {
                        QueryWrapper<CIcCardsInfo> queryWrapper3 = new QueryWrapper<>();
                        queryWrapper3.eq("CARDINF_DELETED", 0);
                        queryWrapper3.eq("DATA_FROM", plantId);
                        queryWrapper3.like("CARDINF_ALIAS", iccardAlias);
                        List<CIcCardsInfo> cIcCardsInfos = iCIcCardsInfoService.list(queryWrapper3);
                        if (cIcCardsInfos.size()>0) {
                            List<String> list = new ArrayList<>();
                            for (CIcCardsInfo cIcCardsInfo : cIcCardsInfos) {
                                list.add(cIcCardsInfo.getCardinfCardno());
                            }
                            queryWrapper.in(  "WEISHT_ICCARDNO", list);
                        }
                    }
                    //        根据品种查询

                    queryWrapper.in(product != null && product.size()>0, "WEISHT_PRODUCT", product);
//        根据车号查询
                    queryWrapper.like(truckno != null && !truckno.equals(""), "WEISHT_TRUCKNO", truckno);
//        根据操作员查询
                    queryWrapper.like(operator != null && !operator.equals(""), "WEISHT_OPERATOR", operator);
//        根据单号查询
                    queryWrapper.eq(sheetno != null && !sheetno.equals(""), "WEISHT_SHEETNO", sheetno);
//        根据1备注查询
                    queryWrapper.like(reason != null && !reason.equals(""), "WEISHT_MODIFY_REASON", reason);
                    queryWrapper.eq("DATA_FROM", plantId);
//        根据是否空车查询
                    if (isEmpty != null) {
                        if (isEmpty == 0) {
//                WEISHT_NET_WGHT大于0表示重车
                            queryWrapper.gt("WEISHT_NET_WGHT", 0);
                        }
                    }
                    List<CWeightSheets> cWeightSheets = cWeightSheetsService.list(queryWrapper);
                    //         先根据List<Map<String, Object>> fields匹配一下需要的列
//                    遍历cWeightSheets匹配到对应的字段
                    for (CWeightSheets cWeightSheet : cWeightSheets) {
//                        创建一个PoundQueryExcelBo对象
                        PoundQueryExcelEn poundQueryExcelEn = PoundQueryExcelEn.builder().build();
//                        将cWeightSheet的值赋值给poundQueryExcelBo
                        poundQueryExcelEn.setWeishtSheetno(cWeightSheet.getWeishtSheetno());
//                        weishtYearSn
                        poundQueryExcelEn.setWeishtYearSn(String.valueOf(cWeightSheet.getWeishtYearSn()));
                        poundQueryExcelEn.setWeishtTruckno(cWeightSheet.getWeishtTruckno());
                        poundQueryExcelEn.setWeishtProduct(cWeightSheet.getWeishtProduct());
                        poundQueryExcelEn.setWeishtGrossWght(new BigDecimal(cWeightSheet.getWeishtGrossWght().toString()));
                        poundQueryExcelEn.setWeishtTare(new BigDecimal(cWeightSheet.getWeishtTare().toString()));
                        poundQueryExcelEn.setWeishtNetWght(new BigDecimal(cWeightSheet.getWeishtNetWght().toString()));
                        poundQueryExcelEn.setWeishtPrice(new BigDecimal(cWeightSheet.getWeishtPrice().toString()));
                        poundQueryExcelEn.setWeishtMoney(new BigDecimal(cWeightSheet.getWeishtMoney().toString()));
                        poundQueryExcelEn.setWeishtCoSupply(cWeightSheet.getWeishtCoSupply());
                        poundQueryExcelEn.setWeishtCo(cWeightSheet.getWeishtCo());
                        poundQueryExcelEn.setWeishtIdentity(cWeightSheet.getWeishtIdentity());
                        poundQueryExcelEn.setWeishtTell(cWeightSheet.getWeishtTell());
                        if (  cWeightSheet.getWeishtMarketPrice() != null) {
                            poundQueryExcelEn.setWeishtMarketPrice(new BigDecimal(String.valueOf(cWeightSheet.getWeishtMarketPrice())));
                        } else {
                            poundQueryExcelEn.setWeishtMarketPrice(BigDecimal.valueOf(0));
                        }
                        if (  cWeightSheet.getWeightSpecialPrice() != null) {
                            poundQueryExcelEn.setWeightSpecialPrice(new BigDecimal(String.valueOf(cWeightSheet.getWeightSpecialPrice())));
                        } else {
                            poundQueryExcelEn.setWeightSpecialPrice(BigDecimal.valueOf(0));
                        }
                        if (  cWeightSheet.getWeishtPriceType() != null) {
                            poundQueryExcelEn.setWeishtPriceType(cWeightSheet.getWeishtPriceType());
                        } else {
                            poundQueryExcelEn.setWeishtMarketPrice(BigDecimal.valueOf(0));
                        }
//                        weishtOutTm
                        if (cWeightSheet.getWeishtOutTm() != null) {
                            poundQueryExcelEn.setWeishtOutTm(cWeightSheet.getWeishtOutTm().toString().replace("T"," "));
                        } else {
                            poundQueryExcelEn.setWeishtOutTm("");
                        }
//                        weishtEmptyTm
                        if (cWeightSheet.getWeishtEnterTm() != null) {
                            poundQueryExcelEn.setWeishtEnterTm(cWeightSheet.getWeishtEmptyTm().toString().replace("T"," "));
                        } else {
                            poundQueryExcelEn.setWeishtEnterTm("");
                        }
                        poundQueryExcelEn.setWeishtEmptyTm(cWeightSheet.getWeishtEmptyTm().toString().replace("T"," "));
                        poundQueryExcelEn.setWeishtFullTm(cWeightSheet.getWeishtFullTm().toString().replace("T"," "));
//                        poundQueryExcelEn.setWeishtOperator(cWeightSheet.getWeishtOperator());
//                        poundQueryExcelBo.setWeishtLoadTm(cWeightSheet.getWeishtLoadTm().toString());可以为空
                        if (cWeightSheet.getWeishtLoadTm() != null) {
                            poundQueryExcelEn.setWeishtLoadTm(cWeightSheet.getWeishtLoadTm().toString().replace("T"," "));
                        } else {
                            poundQueryExcelEn.setWeishtLoadTm("");
                        }
//                        poundQueryExcelBo.setWeishtLoadEndTm(cWeightSheet.getWeishtLoadEndTm().toString());可以为空
                        if (cWeightSheet.getWeishtLoadEndTm() != null) {
                            poundQueryExcelEn.setWeishtLoadEndTm(cWeightSheet.getWeishtLoadEndTm().toString().replace("T"," "));
                        } else {
                            poundQueryExcelEn.setWeishtLoadEndTm("");
                        }
                        if (cWeightSheet.getWeishtBillgross() != null) {
                            poundQueryExcelEn.setWeishtBillgross(new BigDecimal(cWeightSheet.getWeishtBillgross().toString()));
                        }
                        if (cWeightSheet.getWeishtBilltare() != null) {
                            poundQueryExcelEn.setWeishtBilltare(new BigDecimal(cWeightSheet.getWeishtBilltare().toString()));
                        }
                        if  (cWeightSheet.getWeishtBillnet() != null) {
                            poundQueryExcelEn.setWeishtBillnet(new BigDecimal(cWeightSheet.getWeishtBillnet().toString()));
                        }
//                         weishtAshgateName

                        CAshGateVideoBind cAshGateVideoBind = new CAshGateVideoBind();  // 假设这是正确的构造函数

                        CAshGateVideoBind cAshGateVideoBind1 = null;
                        if (cWeightSheet.getWeishtSelAshgateId() != null) {
                            QueryWrapper<CAshGateVideoBind> cAshGateVideoBindQueryWrapper1 = new QueryWrapper<>();
                            cAshGateVideoBindQueryWrapper1.eq("ASHGATE_ID", cWeightSheet.getWeishtSelAshgateId());
                            cAshGateVideoBindQueryWrapper1.eq("ASHGATE_DELETED", 0);
                            cAshGateVideoBind1 = iCAshGateVideoBindService.getOne(cAshGateVideoBindQueryWrapper1);
                        }

                        // 查询实装点
                        CAshGateVideoBind cAshGateVideoBind2 = null;
                        if (cWeightSheet.getWeishtAshgateId() != null) {
                            QueryWrapper<CAshGateVideoBind> cAshGateVideoBindQueryWrapper2 = new QueryWrapper<>();
                            cAshGateVideoBindQueryWrapper2.eq("ASHGATE_ID", cWeightSheet.getWeishtAshgateId());
                            cAshGateVideoBindQueryWrapper2.eq("ASHGATE_DELETED", 0);
                            cAshGateVideoBind2 = iCAshGateVideoBindService.getOne(cAshGateVideoBindQueryWrapper2);
                        }
                        AshGateHandling ashGateHandling = new  AshGateHandling();

                        // 获取产品名称
                        String pdtName = cWeightSheet.getWeishtProduct();
                        if (pdtName == null) {
                            pdtName = "";  // 或者其他默认值
                        }

                        // 获取装载点名称
                        String ashGateName1 = (cAshGateVideoBind1 != null) ? cAshGateVideoBind1.getAshgateName() : "";
                        String ashGateName2 = (cAshGateVideoBind2 != null) ? cAshGateVideoBind2.getAshgateName() : "";

                        // 调用 ashGateHandling 方法
                        String ashGateName = ashGateHandling.ashGateHandling(pdtName, ashGateName1, ashGateName2);

                        // 设置装载点名称
                        cAshGateVideoBind.setAshgateName(ashGateName != null ? ashGateName : "");
                        poundQueryExcelEn.setWeishtAshgateName(cAshGateVideoBind.getAshgateName());
                        poundQueryExcelEn.setWeishtRemark(cWeightSheet.getWeishtRemark());
                        poundQueryExcelEn.setWeishtIccardno(cWeightSheet.getWeishtIccardno());
                        QueryWrapper<CIcCardsInfo> queryWrapper2 = new QueryWrapper<>();
                        queryWrapper2.eq("CARDINF_CARDNO", cWeightSheet.getWeishtIccardno());
                        queryWrapper2.eq("CARDINF_DELETED", 0);
                        CIcCardsInfo cIcCardsInfo = iCIcCardsInfoService.getOne(queryWrapper2);
                        if (cIcCardsInfo != null) {
                            poundQueryExcelEn.setWeishtIccardAlias(cIcCardsInfo.getCardinfAlias());
                        }
//                        存入poundQueryExcelBos
                        poundQueryExcelEns.add(poundQueryExcelEn);
                    }
                    break;
                case "selected":
                    for (Map<String, Object> id : ids) {
                        Map<String, Object> newId = new HashMap<>();
                        for (Map<String, Object> field : fields) {
                            if (id.containsKey(field.get("field"))) {
                                newId.put(field.get("field").toString(), id.get(field.get("field")));
                            }
                        }
                        newId.put("weishtChiCo", id.get("weishtChiCo"));
                        newId.put("weishtCoSupplyChi", id.get("weishtCoSupplyChi"));
                        newIds.add(newId);
                    }
//            newIds为Map<String, Object>类型，需要转换为PoundQueryExcelBo类型
//            遍历newIds
                    for (Map<String, Object> newId : newIds) {
//                创建一个PoundQueryExcelBo对象
                        PoundQueryExcelEn poundQueryExcelEn = PoundQueryExcelEn.builder().build();
//                遍历newId
                        for (Map.Entry<String, Object> entry : newId.entrySet()) {
                           // 判断entry的key是否为"poundDate"，是的话，将entry的value转换为LocalDateTime类型，再赋值给poundQueryExcelBo的poundDate
                            if (entry.getKey().equals("weishtSheetno")) {
                                poundQueryExcelEn.setWeishtSheetno(entry.getValue().toString());
                            }
//                            weishtYearSn
                            if (entry.getKey().equals("weishtYearSn")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtYearSn(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("weishtTruckno")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtTruckno(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("weishtProduct")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtProduct(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("weishtGrossWght")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtGrossWght(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("weishtTare")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtTare(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("weishtNetWght")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtNetWght(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            //                            weishtBillgross
                            if (entry.getKey().equals("weishtBillgross")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtBillgross(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("weishtBilltare")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtBilltare(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("weishtBillnet")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtBillnet(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("weishtPrice")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtPrice(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("weishtMarketPrice")){
                                if ( (entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtMarketPrice(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("weightSpecialPrice")){
                                if ( (entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeightSpecialPrice(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("weishtPriceType")){
                                if ( (entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtPriceType(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("weishtMoney")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtMoney(new BigDecimal(entry.getValue().toString()));
                                }
                            }
                            if (entry.getKey().equals("weishtCoSupply")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtCoSupply(entry.getValue().toString());
                                }
                                //poundQueryExcelEn.setWeishtCoSupply(entry.getValue().toString() + "." + poundQueryExcelEn.getWeishtCoSupply());
                            }
                            if (entry.getKey().equals("weishtCo")) {
                                if ((entry.getValue() != "") && (entry.getValue() != null)) {
                                    poundQueryExcelEn.setWeishtCo(entry.getValue().toString());
                                }
                            }
                            if (entry.getKey().equals("weishtEnterTm")) {
                                if (entry.getValue() != null) {
                                    poundQueryExcelEn.setWeishtEnterTm(entry.getValue().toString().replace("T"," "));
                                } else {
                                    poundQueryExcelEn.setWeishtEnterTm("");
                                }
                            }
                            if (entry.getKey().equals("weishtOutTm")) {
                                if (entry.getValue() != null) {
                                    poundQueryExcelEn.setWeishtOutTm(entry.getValue().toString().replace("T"," "));
                                } else {
                                    poundQueryExcelEn.setWeishtOutTm("");
                                }
                            }
                            if (entry.getKey().equals("weishtEmptyTm")) {
                                if (entry.getValue() != null) {
                                    poundQueryExcelEn.setWeishtEmptyTm(entry.getValue().toString().replace("T"," "));
                                } else {
                                    poundQueryExcelEn.setWeishtEmptyTm("");
                                }

                            }
                            if (entry.getKey().equals("weishtFullTm")) {
                                if (entry.getValue() != null) {
                                    poundQueryExcelEn.setWeishtFullTm(entry.getValue().toString().replace("T"," "));
                                } else {
                                    poundQueryExcelEn.setWeishtFullTm("");
                                }
                            }
                            if (entry.getKey().equals("weishtLoadTm")) {
                                if (entry.getValue() != null) {
                                    poundQueryExcelEn.setWeishtLoadTm(entry.getValue().toString().replace("T"," "));
                                } else {
                                    poundQueryExcelEn.setWeishtLoadTm("");
                                }
                            }
                            if (entry.getKey().equals("weishtLoadEndTm")) {
                                if (entry.getValue() != null) {
                                    poundQueryExcelEn.setWeishtLoadEndTm(entry.getValue().toString().replace("T"," "));
                                } else {
                                    poundQueryExcelEn.setWeishtLoadEndTm("");
                                }
                            }
                            //                            weishtIccardno
                            if (entry.getKey().equals("weishtIccardno")) {
                                if (entry.getValue() != null) {
                                    poundQueryExcelEn.setWeishtIccardno(entry.getValue().toString());
                                } else {
                                    poundQueryExcelEn.setWeishtIccardno("");
                                }
                            }
//                            weishtIccardAlias
                            if (entry.getKey().equals("weishtIccardAlias")) {
                                if (entry.getValue() != null) {
                                    poundQueryExcelEn.setWeishtIccardAlias(entry.getValue().toString());
                                } else {
                                    poundQueryExcelEn.setWeishtIccardAlias("");
                                }
                            }
                            if (entry.getKey().equals("weishtRemark")) {
                                if (entry.getValue() != null) {
                                    poundQueryExcelEn.setWeishtRemark(entry.getValue().toString());
                                } else {
                                    poundQueryExcelEn.setWeishtRemark("");
                                }
                            }
//                            if (entry.getKey().equals("weishtOperator")) {
//                                if (entry.getValue() != null) {
//                                    poundQueryExcelEn.setWeishtOperator(entry.getValue().toString());
//                                } else {
//                                    poundQueryExcelEn.setWeishtOperator("");
//                                }
//                            }
                            //                            weishtAshgateName
                            if (entry.getKey().equals("weishtAshgateName")) {
                                if (entry.getValue() != null) {
                                    poundQueryExcelEn.setWeishtAshgateName(entry.getValue().toString());
                                } else {
                                    poundQueryExcelEn.setWeishtAshgateName("");
                                }
                            }
                        }
//                存入poundQueryExcelBos
                        poundQueryExcelEns.add(poundQueryExcelEn);
                    }
                    break;
                default:
                    break;
            }
            return poundQueryExcelEns;
        } catch (Exception e) {
//使用slf4j打印异常信息：导出poundQueryExcelBos失败
            log.error("导出poundQueryExcelBos失败", e);
            return null;
        }
    }


    //    查询每月的各品种销售情况
    public SzResult getMonthSale(Integer year, String token, String plantId) throws Exception {
//        DATA_FROM
//        String dataFrom = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
        List<MonthlySalesVo> monthlySalesVos = cWeightSheetsMapper.monthlySales(year, plantId);
//         先将年和月拼在一起
        for (MonthlySalesVo monthlySalesVo : monthlySalesVos) {
            monthlySalesVo.setYearMonth(monthlySalesVo.getYear() + "-" + monthlySalesVo.getMonth());
        }
//        按品种做合计
        return SzResult.success(monthlySalesVos);
    }

//        导出发票统计表
//    public class InvoiceStatisticsBo {
//    private String coId;
//    private String supplyCoId;
//    private String pdtId;
//    private String startTime;
//    private String endTime;
//}
public  List<InvoiceStatisticsVo> exportInvoiceStatistics(String coId, String supplyCoId, String pdtId, String startTime, String endTime) throws Exception {
    InvoiceStatisticsBo invoiceStatisticsBo = new InvoiceStatisticsBo();

    if (coId != null && !coId.equals("")) {
        QueryWrapper<CCompanys> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", coId);
        CCompanys cCompanys = cCompanysService.getOne(queryWrapper);
        invoiceStatisticsBo.setCo(cCompanys.getCpyCo());
        invoiceStatisticsBo.setCoChi(cCompanys.getCpyChico());
    }

    if (supplyCoId != null && !supplyCoId.equals("")) {
        QueryWrapper<CCompanys> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("ID", supplyCoId);
        CCompanys cCompanys1 = cCompanysService.getOne(queryWrapper1);
        invoiceStatisticsBo.setSuppLyCo(cCompanys1.getCpyCo());
        invoiceStatisticsBo.setSuppLyChiCo(cCompanys1.getCpyChico());
    }

    if (pdtId != null && !pdtId.equals("")) {
        QueryWrapper<CProductsInfo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("ID", pdtId);
        CProductsInfo cProductsInfo = cProductsInfoService.getOne(queryWrapper2);
        invoiceStatisticsBo.setPdtName(cProductsInfo.getPdtinfPdt());
    }

    invoiceStatisticsBo.setStartTime(startTime);
    invoiceStatisticsBo.setEndTime(endTime);

    // 从数据库获取发票统计数据
    List<InvoiceStatisticsVo> invoiceStatisticsVos = cWeightSheetsMapper.exportInvoiceStatistics(invoiceStatisticsBo);

    // 检查 pdtName 是否包含 '.'，并在第一时间将 coName 与 supplyCoName 互换
    invoiceStatisticsVos = invoiceStatisticsVos.stream()
            .peek(vo -> {
                if (vo.getPdtName() != null && vo.getPdtName().contains(".")) {
                    String temp = vo.getCoName();
                    vo.setCoName(vo.getSupplyCoName());
                    vo.setSupplyCoName(temp);
                }
            })
            .peek(vo -> {
                // 检查 priceType 是否为空，如果不为空则拼接到 pdtName 后面
                if (vo.getPriceType() != null && !vo.getPriceType().isEmpty()) {
                    vo.setPdtName(vo.getPdtName() + " (" + vo.getPriceType() + ")");
                }
            })
            .peek(vo -> {
                // 计算 totalNetWght 并四舍五入
                BigDecimal netWght = vo.getTotaGrossWght().subtract(vo.getTotaTare());
                vo.setTotalNetWght(netWght.setScale(2, BigDecimal.ROUND_HALF_UP));

                // 计算 totalMoney 并四舍五入
                BigDecimal money = vo.getTotalNetWght().multiply(vo.getPrice());
                vo.setTotalMoney(money.setScale(2, BigDecimal.ROUND_HALF_UP));
            })
            .collect(Collectors.toList());

    // 添加汇总行
    BigDecimal totalTotaTare = invoiceStatisticsVos.stream()
            .map(InvoiceStatisticsVo::getTotaTare)
            .reduce(BigDecimal.ZERO, BigDecimal::add)
            .setScale(2, BigDecimal.ROUND_HALF_UP);

    BigDecimal totalTotaGrossWght = invoiceStatisticsVos.stream()
            .map(InvoiceStatisticsVo::getTotaGrossWght)
            .reduce(BigDecimal.ZERO, BigDecimal::add)
            .setScale(2, BigDecimal.ROUND_HALF_UP);

    BigDecimal totalTotalNetWght = invoiceStatisticsVos.stream()
            .map(InvoiceStatisticsVo::getTotalNetWght)
            .reduce(BigDecimal.ZERO, BigDecimal::add)
            .setScale(2, BigDecimal.ROUND_HALF_UP);

    BigDecimal totalTotalMoney = invoiceStatisticsVos.stream()
            .map(InvoiceStatisticsVo::getTotalMoney)
            .reduce(BigDecimal.ZERO, BigDecimal::add)
            .setScale(2, BigDecimal.ROUND_HALF_UP);



    // 按照公司名称排序，如果公司名称相同，则按照 supplyCoName 排序，最后按照产品名称排序
    invoiceStatisticsVos.sort(Comparator.comparing(InvoiceStatisticsVo::getCoName)
            .thenComparing(InvoiceStatisticsVo::getSupplyCoName)
            .thenComparing(InvoiceStatisticsVo::getPdtName));

    InvoiceStatisticsVo summaryRow = new InvoiceStatisticsVo();
    summaryRow.setCoName("合计");
    summaryRow.setSupplyCoName("");
    summaryRow.setPdtName("");
    summaryRow.setTotaTare(totalTotaTare);
    summaryRow.setTotaGrossWght(totalTotaGrossWght);
    summaryRow.setTotalNetWght(totalTotalNetWght);
    summaryRow.setTotalMoney(totalTotalMoney);

    invoiceStatisticsVos.add(summaryRow);

    // 返回结果
    return invoiceStatisticsVos;
}

//导出发票统计表格
public List<InvoiceStatisticsExcelEn> exportInvoiceStatisticsExcel(List<InvoiceStatisticsVo> invoiceStatisticsVos) throws Exception {
    return invoiceStatisticsVos.stream()
            .map(this::convertToExcelFormat)
            .collect(Collectors.toList());
}

    private InvoiceStatisticsExcelEn convertToExcelFormat(InvoiceStatisticsVo vo) {
        InvoiceStatisticsExcelEn excelEn = new InvoiceStatisticsExcelEn();
        excelEn.setCoName(vo.getCoName());
        excelEn.setSupplyCoName(vo.getSupplyCoName());
        excelEn.setPdtName(vo.getPdtName());
        excelEn.setPrice(vo.getPrice());
        excelEn.setTotaTare(vo.getTotaTare());
        excelEn.setTotaGrossWght(vo.getTotaGrossWght());
        excelEn.setTotalNetWght(vo.getTotalNetWght());
        excelEn.setTotalMoney(vo.getTotalMoney());
        return excelEn;
    }
}


